// pages/commit/commit.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    textContent: '',
    name:"微信用户",
    avatar:"",
    openid:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    //从缓存中读取用户信息
    var userInfo = wx.getStorageSync('userInfo')
    if (userInfo) {
        this.setData({
            name: userInfo.nickName,
            avatar: userInfo.avatarUrl,
            openid: userInfo.openid
        })
      console.log("userInfo:" , userInfo)
    } else {
      console.log("no userInfo")
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  post:function (){
      //读取缓存中的openid
        var openid = wx.getStorageSync('openid')
     if (openid && this.data.textContent) {
         // 获取当前时间
         var date = new Date();
         var year = date.getFullYear();
         var month = date.getMonth() + 1;
         var day = date.getDate();
         var hour = date.getHours();
         var minute = date.getMinutes();
         //拼接时间
         var time = year + "-" + month + "-" + day + " " + hour + ":" + minute;
         var content = this.data.textContent;
         //读取缓存中的openid
         var openid = wx.getStorageSync('openid')
         console.log(typeof openid)
         //将content转换为字符串
         var content = JSON.stringify(content)
         console.log(typeof content)
         //从本地缓存中获取头像和昵称
         var userInfo = wx.getStorageSync('userInfo')
         var avatar = userInfo.avatarUrl
         var name = userInfo.nickName
         //只匹配昵称中的汉字字母数字符号
        //  name = /[\u4e00-\u9fa5a-zA-Z0-9]/g;
         console.log("name:", name)
         var data = {
                "avatar": avatar,
                "name": name,
                "openid": openid,
                "content": content,
         }
         //上传数据
         wx.request({
             url: 'http://127.0.0.1:3000/login/commit_post',
             header: { 'Content-Type': 'application/json;charset=utf-8' },
             data: data,
             method: 'POST',
             success(res) {
              console.log(openid,content);
               //上传成功后，提示用户
                    wx.showToast({
                        title: '发布成功',
                        icon: 'success',
             })
                }
         })
     } else {
         //提示用户授权,跳转到mine页面
         wx.showModal({
             title: '提示',
             content: '请先授权登录',
             success: function (res) {
                 if (res.confirm) {
                     wx.switchTab({
                         url: '../mine/mine',
                     })
                 }
             }
         })
     }
  },

  /**
   * 预览图片
   */
  previewImage: function (event) {
    let url = event.target.id;
    wx.previewImage({
      current: '',
      urls: [url]
    })
  },

  /**
   * 设置是否匿名
   */
  setPrivate: function (event) {
    this.setData({
      private: event.detail.value
    });
  },

  /**
   * 获取输入内容
   */
  getTextContent: function (event) {
    //读取本地缓存中的openid
    var openid = wx.getStorageSync('openid')
      console.log("openid:", openid)
    //判断page中是否有userInfo
    if (openid) {
        this.setData({
            textContent: event.detail.value,
        });
    } else {
        //提示用户授权,跳转到mine页面
        wx.showModal({
            title: '提示',
            content: '请先授权登录',
            success: function (res) {
                if (res.confirm) {
                    wx.switchTab({
                        url: '../mine/mine',
                    })
                }
            }
})
    }
  }
})